package freedoms.yousister.user;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import freedoms.yousister.R;
import freedoms.yousister.common.Contants;
import freedoms.yousister.common.HttpClientUtil;
import freedoms.yousister.response.RegistUsernameResponse;

public class RegistUsernameActivity extends Activity {
	private Button leftButton;
	private TextView centerTextView;
	private Button rightButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.regist_username_layout);

		leftButton = (Button) findViewById(R.id.leftButton);
		leftButton.setVisibility(View.INVISIBLE);

		centerTextView = (TextView) findViewById(R.id.centerTextView);
		centerTextView.setText("YOUSISTER");

		rightButton = (Button) findViewById(R.id.rightButton);
		rightButton.setVisibility(View.INVISIBLE);

		findViewById(R.id.okButton).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				RegistUsernameAction();
			}
		});
	}

	private void RegistUsernameAction() {
		String username = ((EditText) findViewById(R.id.username)).getText().toString();
		if (username != null && username != "") {
			HttpClientUtil httpClientUtil = new HttpClientUtil();
			List<NameValuePair> parameters = new ArrayList<NameValuePair>();
			Intent intent = getIntent();
			String mobile = intent.getStringExtra("mobile");
			final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
			String device_id = tm.getDeviceId();
			parameters.add(new BasicNameValuePair("mobile", mobile));
			parameters.add(new BasicNameValuePair("username", username));
			parameters.add(new BasicNameValuePair("device_id", device_id));
			System.out.println("mobile＝＝＝＝" + mobile);
			System.out.println("username＝＝＝＝" + username);
			System.out.println("device_id＝＝＝＝" + device_id);
			HttpResponse response = httpClientUtil.post(Contants.REGIST_URL, parameters);
			System.out.println(response.getStatusLine().getStatusCode());
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				try {
					String message = EntityUtils.toString(response.getEntity(), "utf-8");
					System.out.println(message);
					Gson gson = new Gson();
					RegistUsernameResponse registUsernameResponse = gson
							.fromJson(message, RegistUsernameResponse.class);
					if (Contants.RES_SUCCESS.equals(registUsernameResponse.getStatusCode())) {
						Intent intent1 = new Intent();
						intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
						intent1.setClass(RegistUsernameActivity.this, UserListActivity.class);
						startActivity(intent1);
						RegistUsernameActivity.this.finish();
					} else if (Contants.RES_USERNAME_INVALID.equals(registUsernameResponse.getStatusCode())) {
						Toast.makeText(RegistUsernameActivity.this, registUsernameResponse.getMessage(),
								Toast.LENGTH_SHORT).show();
					} else if (Contants.RES_DEVICE_ID_INVALID.equals(registUsernameResponse.getStatusCode())) {
						Toast.makeText(RegistUsernameActivity.this, registUsernameResponse.getMessage(),
								Toast.LENGTH_SHORT).show();
					}
				} catch (ParseException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			} else {
				Toast.makeText(RegistUsernameActivity.this, "访问出错了，请联系管理员", Toast.LENGTH_SHORT).show();
			}
		} else {
			Toast.makeText(RegistUsernameActivity.this, "请填写用户名", Toast.LENGTH_SHORT).show();
		}
	}
}
